﻿/*************************************************
版权所有(C), 2012, 广州新昊网络科技有限公司
文件名称： MatrixCard.cs 
作者: 卢盛福
QQ：34885177
完成日期: 2012/8/7 14:39:29
环境版本：4.0.30319.239
主要内容:详细说明此程序文件完成的功能
-----------------第一次修改-----------------
修改作者：
QQ：
修改日期：
环境版本：
修改内容：
*************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CrazyTalk.Common
{
    ///<summary> 
    ///类名:MatrixCard		 
    ///作者:卢盛福	 
    ///QQ:34885177
    ///完成日期: 2012/8/7 14:39:29	 
    ///功能描述:MatrixCard实体类	
    /// -----------------第一次修改-----------------
    /// 修改作者:		
    ///QQ: 
    /// 修改日期:	
    /// 修改描述:		
    ///</summary>

    public class MatrixCards
    {
        ///<summary>
        /// 单元格集合
        ///</summary>
        public List<Coordinate> Coordinate
        {
            get;
            set;
        }
 
        ///<summary>
        /// 随机选择单元格让用户验证
        ///</summary>
        ///<param name="howMany">单元格个数</param>
        ///<param name="colUpper">上界</param>
        ///<param name="rowUpper">下界</param>
        ///<returns>Cell集合</returns>
        public List<Coordinate> PickUpRandomCells(int howMany, int colUpper, int rowUpper)
        {
            Random r = new Random();
            List<Coordinate> cells = new List<Coordinate>();
            for (int i = 0; i < howMany; i++)
            {
                int randomCol = r.Next(0, colUpper);
                int randomRow = r.Next(0, rowUpper);
                Coordinate c = new Coordinate(randomRow, randomCol);
                cells.Add(c);
            }
            return cells;
        }
        ///<summary>
        /// 生成随机矩阵
        ///</summary>
        ///<param name="rows">行数</param>
        ///<param name="cols">列数</param>
        ///<returns>矩阵数组</returns>
        public static int[,] GenerateRandomMatrix(int rows, int cols)
        {
            Random r = new Random();
            int[,] arr = new int[8, 5];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    arr[i, j] = r.Next(10, 100);//0-100的范围
                }
            }
            return arr;
        }
        ///<summary>
        /// 将矩阵数组转换为Coordinate集合
        ///</summary>
        ///<param name="array">矩阵数组</param>
        ///<returns>Coordinate集合</returns>
        public static List<Coordinate> ConvertMatrixArrayToCellList(int[,] array)
        {
            List<Coordinate> list = new List<Coordinate>();
            int[] linearr = new int[40];
            int k = 0;
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    linearr[k] = array[i, j];
                    Coordinate codn = new Coordinate(i, j);
                    codn.Value = linearr[k];
                    list.Add(codn);
                    k++;
                }
            }
            return list;
        }
        ///<summary>
        /// 由矩阵字符串生成矩阵数组
        ///</summary>
        ///<param name="matrixStr">矩阵字符串</param>
        ///<returns>矩阵数组</returns>
        public static int[,] ConvertStringToMatrixArray(string matrixStr)
        {
            int[,] arr = new int[8, 5];
            int[] tempArr = new int[40];
            int k = 0;
            string[] tempArrStr = matrixStr.Split(',');
            for (int i = 0; i < tempArr.Length; i++)
            {
                tempArr[i] = Convert.ToInt32(tempArrStr[i]);
            }
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    arr[i, j] = tempArr[k];
                    k++;
                }
            }
            return arr;
        }
    }
    [Serializable]
    public class Coordinate
    {
        ///<summary>
        /// 坐标
        ///</summary>
        public string ColCode
        {
            get;
            set;
        }
        ///<summary>
        /// 值
        ///</summary>
        public int Value
        {
            get;
            set;
        }

        ///<summary>
        /// 列名枚举
        ///</summary>
        public enum ColCodeEnum
        {
            A = 1,
            B = 2,
            C = 3,
            D = 4,
            E = 5
        };
        /// <summary>
        /// 计算坐标
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="colIndex"></param>
        public Coordinate(int rowIndex, int colIndex)
        {
            this.ColCode = ConvertUtil.ToString(GetColCode(colIndex + 1)) + (rowIndex + 1);
            this.Value = 0;
        }

        ///<summary>
        /// 根据列号获取列名
        ///</summary>
        ///<param name="colIndex">列号</param>
        ///<returns>列名</returns>
        public static ColCodeEnum GetColCode(int colIndex)
        {
            ColCodeEnum colCode = ColCodeEnum.A;
            switch (colIndex)
            {
                case 1:
                    colCode = ColCodeEnum.A;
                    break;
                case 2:
                    colCode = ColCodeEnum.B;
                    break;
                case 3:
                    colCode = ColCodeEnum.C;
                    break;
                case 4:
                    colCode = ColCodeEnum.D;
                    break;
                case 5:
                    colCode = ColCodeEnum.E;
                    break;
                default:
                    break;
            }
            return colCode;
        }
    }
}
